//
//  GetFeedCommentsRequest.m
//  collection
//
//  Created by zhangjing on 4/26/12.
//  Copyright (c) 2012 Tsinghua University. All rights reserved.
//

#import "GetFeedCommentsRequest.h"

@implementation GetFeedCommentsRequest

- (void)initParams {
    
    self.descriptionName = @"note.getcomments";
    
    RequestData* requestData = [[GetFeedCommentRequestData alloc] init];
    self.request = requestData;
    [requestData release];
    
    Response* response = [[GetFeedCommentResponse alloc] init];
    self.response = response;
    [response release];
}

- (NSString*)buildRequestURL {
    
    GetFeedCommentRequestData* requestData = (GetFeedCommentRequestData*)self.request;
    
    NSString* requestContent = [NSString stringWithFormat:@"&uid=%lld&noteId=%lld&pageIndex=%d&sinceCommentId=%lld",
                                requestData.UID,
                                requestData.FID,
                                requestData.page,
                                requestData.sinceFID];
    
    return requestContent;
}

- (void)parseResponse:(NSObject *)data {
    
    GetFeedCommentResponse* response = (GetFeedCommentResponse*)self.response;
    
    NSDictionary* dic = (NSDictionary*)data;
    
    NSString* value = nil;
    
    // page
    value = [dic objectForKey:@"pageIndex"];
    if (value != nil && ![value isEqualToString:@""]) {
        response.page = value.intValue;
    }
    
    // FID
    value = [dic objectForKey:@"noteId"];
    if (value != nil && ![value isEqualToString:@""]) {
        response.FID = value.longLongValue;
    }
    
    // Comments
    NSArray* comments = [dic objectForKey:@"comments"];
    NSMutableArray* commentList = [[NSMutableArray alloc] initWithCapacity:1];
    for (NSDictionary* oneComment in comments) {
        Feed* newFeed = [self parseOneFeed:oneComment];
        if (newFeed != nil) {
            [commentList addObject:newFeed];
            [newFeed release];
        }
    }
    
    response.commentList = commentList;
    [commentList release];
}

@end
